package com.pubinfo.passbook.api.controller.system;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.pubinfo.passbook.common.entity.TRabbitmqOrgConvert;
import com.pubinfo.passbook.common.entity.TRabbitmqPersonConvert;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgConvertMapper;
import com.pubinfo.passbook.common.model.dto.system.OrgTreeColleagueListParam;
import com.pubinfo.passbook.common.model.dto.system.OrgTreePersonListParam;
import com.pubinfo.passbook.common.model.dto.system.PersonListParam;
import com.pubinfo.passbook.common.model.vo.OrgRole;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.model.vo.persion.RabbitmqPersonConvertVO;
import com.pubinfo.passbook.common.service.master.TRabbitmqOrgConvertService;
import com.pubinfo.passbook.common.service.master.TRabbitmqPersonConvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zhouch
 * @date 2021/10/28 10:14
 */
@RestController
@RequestMapping("org/tree")
@Api(tags = "人员组织树")
public class OrgTreeController {
  
  @Resource
  TRabbitmqOrgConvertService orgConvertService;
  @Resource
  TRabbitmqOrgConvertMapper convertMapper;
  @Resource
  TRabbitmqPersonConvertService personConvertService;
  
  @GetMapping("root")
  @ApiOperation("获得最上级组织")
  public JsonResult<TRabbitmqOrgConvert> getOrgTreeRoot() {
    return JsonResult.success(orgConvertService.getOrgTreeRoot());
  }
  
  @GetMapping("company/{orgId}")
  @ApiOperation("根据父组织ID获取子组织")
  public JsonResult<List<TRabbitmqOrgConvert>> getSubOrg(@PathVariable("orgId") Long orgId) {
    return JsonResult.success(orgConvertService.getSubOrgsBySuperOrgId(orgId));
  }
  
  @PostMapping("persons_by_org")
  @ApiOperation("根据组织ID获得所属所有人员")
  public JsonResult<MyPage<TRabbitmqPersonConvert>> getPersonByOrg(@RequestBody @Valid OrgTreePersonListParam params) {
    return JsonResult.page(personConvertService.getPersonsByOrgId(params));
  }
  
  @PostMapping("colleague")
  @ApiOperation("根据人员ID查询所在部门所有员工")
  public JsonResult<MyPage<TRabbitmqPersonConvert>> getOrgPersonsByPersonId(@RequestBody @Valid OrgTreeColleagueListParam params) {
    return JsonResult.page(personConvertService.getPersonsByPersonOrgId(params));
  }
  
  @GetMapping("search_org_biz_name/{orgBizName}")
  @ApiOperation("根据orgBizName名称查询组织")
  public JsonResult<List<OrgRole>> searchOrgBizName(@PathVariable("orgBizName") String orgBizName) {
    return JsonResult.success(convertMapper.findOrgByOrgName(orgBizName));
  }
  
  @GetMapping("search_org_name/{orgName}")
  @ApiOperation("根据名称查询组织")
  public JsonResult<List<TRabbitmqOrgConvert>> searchOrgsByName(@PathVariable("orgName") String orgName) {
    return JsonResult.success(orgConvertService.searchOrgsByKeyword(orgName));
  }
  
  @GetMapping("org_relation/{orgId}")
  @ApiOperation("根据组织ID查询该组织及其上级关系")
  public JsonResult<TRabbitmqOrgConvert> getOrgRelation(@PathVariable("orgId") Long orgId) {
    return JsonResult.success(orgConvertService.getOrgWithSuperRelation(orgId));
  }
  
  @GetMapping("org/{orgId}")
  @ApiOperation("根据组织ID查询该组织信息")
  public JsonResult<TRabbitmqOrgConvert> getOrg(@PathVariable("orgId") Long orgId) {
    return JsonResult.success(orgConvertService.getById(orgId));
  }
  
  @PutMapping("audit_user/{acct}/{orgId}")
  @ApiOperation("编辑部门报账员")
  public JsonResult<Boolean> auditUserEdit(@PathVariable("orgId") Long orgId, @PathVariable("acct") String acct) {
    Boolean updateChain = new LambdaUpdateChainWrapper<>(convertMapper).in(TRabbitmqOrgConvert::getOrgId, orgId).set(TRabbitmqOrgConvert::getAuditUser, acct).update();
    return JsonResult.success(updateChain);
  }
  
  @PostMapping("person")
  @ApiOperation("人员列表")
  public JsonResult<MyPage<RabbitmqPersonConvertVO>> getPersonListByKeyword(@RequestBody @Valid PersonListParam params) {
    return JsonResult.page(personConvertService.searchPersonsByKeyword(params));
  }
}
